import cv2, time, random, os, datetime
import os, sys, pyautogui, traceback
import numpy as np
import mss
import action
import random

# from datetime import datetime
mode = 1
# 检测系统
print('操作系统:', sys.platform)
if sys.platform == 'darwin':
    scalar = True
else:
    scalar = False

# 读取文件 精度控制   显示名字
imgs = action.load_imgs()
# pyautogui.PAUSE = 0.05
pyautogui.FAILSAFE = False

start_time = time.time()
# print('程序启动，现在时间', time.ctime())

# 截屏，并裁剪以加速
upleft = (0, 0)
if scalar == True:
    downright = (1136, 700)
else:
    downright = (1900, 1080)
a, b = upleft
c, d = downright
monitor = {"top": b, "left": a, "width": c, "height": d}
# print(monitor)
start = time.time()
accounts = {
    # 'account_1_qiang': ['1_1_piaoliu', '1_2_qingshan', '1_3_wuming', '1_4_linyuan'],
    'account_2_jian': ['2_1_anning', '2_2_lvshui', '2_3_anning_xiao', '2_4_hexi'],
}
actions = [ 'takeJiaCheng', 'takeTili', 'yinyangliao', 'fengmo',  'email']

# constants
last_click = None


# 以上启动，载入设置
##########################################################
def select_mode():
    global start
    end = time.time()
    hours, rem = divmod(end - start, 3600)
    minutes, seconds = divmod(rem, 60)
    print("运行时间：{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), seconds))
    print(datetime.datetime.now())
    # common_task()
    # yinyangliao()
    huanhao()
    # click("account_2_jian")
    # click("sub_account_2_1_anning")
    # click("sub_account_2_2_lvshui")
    # click("sub_account_2_3_anning_xiao")
    # click("sub_account_2_4_hexi")


    return


def click2( ww=0, hh=0, sec=0):
    action.click([ww, hh])
    t2 = random.randint(140, 160) / 100
    time.sleep(t2 + sec)
    return True

def click(img, sec=0, break_count=3):
    for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]:
        screen = action.screenshot(monitor)
        want = imgs[img]
        pts = action.locate(screen, want, 0)
        if not len(pts) == 0:
            size = want[0].shape
            h, w, ___ = size
            xx = action.cheat(pts[0], w, h)
            time.sleep(1.5)
            action.click(xx)
            t2 = random.randint(140, 160) / 100
            time.sleep(t2 + sec)
            time.sleep(1.5)
            return True
        else:
            if i == break_count:
                print("没有找到" + img)
                return False
            time.sleep(.5)


# 领取体力
def yyl_tili():
    print("begin领取体力")
    click("yyl_tili_1")  # 点击体力
    click("yyl_tili_2_take")  # 取出体力
    click("yyl_tili_3_close")  # 关闭蒙板
    last = click("yyl_tili_4_close")  # 关闭领取体力
    if last:
        print("领取体力成功")
    else:
        print("！！！！领取体力失败！！！！")


# 领取勾玉
def yyl_gouyu():
    print("begin领取勾玉")
    last = click("yyl_gouyu_1")
    if last:
        print("领取勾玉成功")
    else:
        print("！！！！领取勾玉失败！！！！")


# 放结界卡
def yyl_put_card():
    print("begin放结界卡")
    click("yyl_put_card_1_begin")  # 开始放结界卡
    click("yyl_put_card_2_all")  # 点击全部
    click("yyl_put_card_3_select_all_taigu")  # 选择所有太鼓
    click("yyl_put_card_4_select_one_taigu")  # 选择其中一个太鼓
    click("yyl_put_card_5_active_taigu")  # 激活太鼓
    last = click("yyl_put_card_6_end")  # 关闭
    if last:
        print("领取勾玉成功")
    else:
        print("！！！！领取勾玉失败！！！！")


# 寄养
def yyl_jiyang():
    for i in [1, 2, 3, 4, 5, 6]:
        if click("yyl_jiyang_1_begin"):  # 寄养开始
            break
        else:
            continue
    click("yyl_jiyang_2_entry")  # 进入寄养
    success = click("yyl_jiyang_3_select_gouyu")  # 选择勾玉
    if not success:
        success = click("yyl_jiyang_4_select_tili")  # 选择体力
    if not success:
        success = click("yyl_jiyang_5_select_jinbi")  # 选择金币
    if success:
        click("yyl_jiyang_6_entry_jiejie")
        click("yinyangliao_12_4_fangshishen")
        click("yinyangliao_12_5_fangshishenjieshu")
        click("yinyangliao_12_6_fanhui")
        click("yinyangliao_12_7_fanhui")
        click("yinyangliao_12_8_fanhui")
        click("yinyangliao_16_fanhui")

    else:
        click("yinyangliao_13_fail_fanhui")
        click("yinyangliao_14_fanhui")
        click("yinyangliao_15_fanhui", 2)
        click("yinyangliao_16_fanhui")

        print(2)
    click("yinyangliao_16_fanhui")


def yinyangliao():
    click("yinyangliao_1_zhankai",2)
    # click2(1400, 620,2) #展开
    click("yinyangliao_2_jinru",4)
    # click2(600, 620, 2)  # 展开
    click("yinyangliao_3_jinrujiejie",4)
    yyl_tili()
    yyl_gouyu()
    yyl_put_card()
    click("yinyangliao_12_6_fanhui", 10)
    click("yinyangliao_16_fanhui", 4)




# 点击不好确认的图片
def mutil_click(imgs, sec=0, break_count=15):
    # random.shuffle(imgs)
    for img in imgs:
        success = click(img, sec, break_count)
        if success:
            return success
    return False


def switch(account, sub_account):
    t = random.randint(4, 5)
    time.sleep(t)
    for i in ['switch_account_1_1_avater', 'switch_account_1_2_avater', 'switch_account_1_3_avater',
              'switch_account_1_4_avater']:  # 点击头像
        if click(i):
            break
    click("switch_account_2_user_center")  # 用户中心
    imgs = ["switch_account_3_switch_account_6"]
    mutil_click(imgs, 2, 3) # 点击切换账号按钮
    # click("switch_account_3_switch_account", 2)  # 点击切换账号按钮
    click("switch_account_4_click_wangyi")  # 点击网易图标选择账号
    click(account)  # 选择主账号
    click("switch_account_5_login",8)  # 登录
    if mode == 1:
        imgs = ["switch_account_6_switch", "switch_account_6_1_switch", "switch_account_6_2_switch"
        , "switch_account_6_3_switch", "switch_account_6_4_switch", "switch_account_6_5_switch"]
        mutil_click(imgs, 0, 25)  # 开始切换
    else:
        click2(random.randint(560, 878), random.randint(508, 535))
    t = random.randint(5, 6)
    time.sleep(t)
    click("sub_account_" + sub_account)  # 选择子账号名
    click("sub_account_" + sub_account + "_f")  # 选择子账号服务器
    if mode == 1:
        imgs = ["switch_account_7_1_entry", "switch_account_7_2_entry", "switch_account_7_3_entry"
            , "switch_account_7_4_entry"]
        mutil_click(imgs, 0, 25)  # 点击进入游戏
    else:
        click2(random.randint(700, 870), random.randint(560, 600))  # 点击进入游戏
    t = random.randint(6, 8)
    time.sleep(t)
    click("sub_account_" + sub_account + "_avater")  # 点击子账号头像
    t = random.randint(10, 13)
    if mode == 1:
        success = click("switch_account_8_click_screen", t)  # 点击屏幕
    else:
        success = click2(random.randint(700, 870), random.randint(560, 600))  # 点击屏幕
        t = random.randint(6, 8)
        time.sleep(t)
    if success:
        print("切换账号成功：" + account + "=>" + sub_account)
    else:
        print("切换账号失败！！！！！：" + account + "=>" + sub_account)


def common_task():
    random.shuffle(actions)
    #签到
    click("qiandao")
    click("qiandao2")
    if click("qiandao3"):
        print("签到成功")
    else:
        print("签到失败")
    #领取勾玉
    if click("takeGouYu"):
        if mode == 1:
            click2(random.randint(700, 870), random.randint(560, 600))  # 关闭勾玉
        else:
            print(1)
        print("领取勾玉成功")
    else:
        print("！！！！领取勾玉失败！！！！")

    # ['takeGouYu','takeJiaCheng','takeTili','yinyangliao',fengmo]
    for i in actions:
        if i == 'takeGouYu':
            if click(i):
                print("领取勾玉成功")
            else:
                print("！！！！领取勾玉失败！！！！")
        if i == 'takeJiaCheng':
            if mode == 1:
                if click(i):
                    click("querenlingqu")
                    success = click("tingyuan")
                    if success:
                        print("领取加成功")
                    else:
                        print("！！！！领取加失败！！！！")
            else:
                if click2(330, 400):
                    click2(710, 460)  # 确认领取
                    click2(710, 500)  # 关闭领取
                    print("领取加成功")

        if i == 'takeTili':
            if click(i):
                print("领取体力成功")
            else:
                print("！！！！领取体力失败！！！！")
        if i == 'email':
            click("email")
            click("email2")
            click("email3")
            click("email4")
            if click("email5"):
                print("领取邮箱成功")
            else:
                print("领取邮箱失败！！！！")
        if i == 'qiandao':
            click("qiandao")
            click("qiandao2")
            if click("qiandao3"):
                print("签到成功")
            else:
                print("签到失败")


def switchAccount(account, sub_account):
    print(sub_account)

    switch(account, sub_account)
    # common_task()
    # yinyangliao()
    return



def huanhao():
    for account, values in accounts.items():
        for subAccount in values:
            switchAccount(account, subAccount)


########################################################
def fengmo():
    if not click("fengmo_1_start", 0, 25):  # 封魔入口
        click("fengmo_2_douji", 0, 25)  # 如果封魔入口图标换成斗技图标
    click("fengmo_3_canjia")  # 参加封魔
    click("fengmo_4_qianwang")  # 前往封魔
    click("fengmo_5_dianji")  # 点击第一次
    t = random.randint(2, 3)
    time.sleep(t)
    click("fengmo_5_dianji")  # 点击第二次
    t = random.randint(2, 3)
    time.sleep(t)
    click("fengmo_5_dianji")  # 点击第三次
    t = random.randint(2, 3)
    time.sleep(t)
    click("fengmo_5_dianji")  # 点击第四次
    t = random.randint(2, 3)
    time.sleep(t)
    for i in [1, 2]:  # 再随机点1到2次
        click("fengmo_5_dianji")
        if i == random.randint(1, 2):
            break
    click("fengmo_6_jiangli")  # 领取奖励
    click("fengmo_7_lingqujianglichenggong")  # 领取成功

    for i in [1, 2, 3]:
        click("fengmo_8_xunzhaoshouling")  # 寻找首领
        dayOfWeek = datetime.datetime.now().isoweekday()
        click("fengmo_9_guaishou" + str(dayOfWeek))  # 点击首领
        click("fengmo_10_jijietiaozhan")  # 集结挑战
        click("fengmo_11_jijiequeding")  # 集结确定
        t = random.randint(2, 3)
        time.sleep(t)
        meiyouzhaodao = click("fengmo_11_1_meiyouzhaodaoguaishou")  # 寻找下个首领
        if not meiyouzhaodao:
            break

    click("fengmo_11_jijiequeding") #开始战斗
    t = random.randint(400, 420)
    time.sleep(t)

    click("fengmo_12_1_shengli")
    click("fengmo_12_chenggong")
    click("fengmo_13_fanhui")
    click("fengmo_13_chenggong")
    click("fengmo_14_chenggong")
    click("fengmo_15_fanhui")
    click("fengmo_16_quedingtuicu")
    click("fengmo_18_fanhui")

####################################################
if __name__ == '__main__':
    select_mode()
